﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/**
 * 功能:
 *      读取windows依赖类
 * 
 * **/

namespace ABFW
{
    public class ABManifestLoader : System.IDisposable
    {
        //本类实例
        private static ABManifestLoader _Instance;
        //锁
        private static object _lock = new object();

        //AssetBundle(清单文件)系统类
        private AssetBundleManifest _ManifestObj;
        //AssetBundle 清单文件的路径
        private string _StrManifestPath;
        //读取AB清单文件的AssetBundle
        private AssetBundle _ABReadManifest;
        //是否加载(Manifest)完成
        private bool _IsLoadFinish;
        /*只读属性*/
        public bool IsLoadFinish {
            get
            {
                return _IsLoadFinish;
            }
        }

        private ABManifestLoader()
        {
            //确定清单文件WWW的下载路径
            _StrManifestPath = PathTools.GetWWWPath() + "/" + PathTools.GetPlatformName();
            _ManifestObj = null;
            _ABReadManifest = null;
            _IsLoadFinish = false;
        }

        /// <summary>
        /// 双重锁获取单例对象
        /// </summary>
        public static ABManifestLoader Instance
        {
            get
            {
                if(_Instance == null)
                {
                    lock (_lock)
                    {
                        if(_Instance == null)
                        {
                            _Instance = new ABManifestLoader();
                        }
                    }
                }
                return _Instance;
            }
        }

        /// <summary>
        /// 加载Manifest清单文件
        /// </summary>
        /// <returns></returns>
        public IEnumerator LoadManifestFile()
        {
            using (WWW www=new WWW(_StrManifestPath))
            {
                yield return www;
                if(www.progress >= 1)
                {
                    //加载完成,获取AssetBundle实例
                    AssetBundle abObj = www.assetBundle;
                    if(abObj != null)
                    {
                        _ABReadManifest = abObj;
                        //读取清单文件(读取到系统类的实例中)
                        //"AssetBundleManifest"是固定写法,获取到打包后自动生成的清单文件
                        _ManifestObj = _ABReadManifest.LoadAsset(ABDefine.ASSETBUNDLE_MANIFEST) as AssetBundleManifest;
                        //本次加载与读取清单文件
                        _IsLoadFinish = true;
                    }else
                    {
                        Debug.LogError(GetType() + "/LoadManifestFile() 下载出错, _StrManifestPath = " + _StrManifestPath + " 错误信息: "+www.error);
                    }
                }
            }
        }

        /// <summary>
        /// 获取加载完成后的AssetBundleManifest系统类实例
        /// </summary>
        public AssetBundleManifest GetManifest
        {
            get
            {
                if (_IsLoadFinish)
                {
                    if (_ManifestObj != null)
                    {
                        return _ManifestObj;
                    }
                    Debug.LogError(GetType() + "/GetManifest/, _manifestObj为Null");
                }else
                {
                    Debug.LogError(GetType() + "/GetManifest/, _manifestObj未加载");
                }
                return null;
            }
        }

        /// <summary>
        /// 获取AB包下所有的依赖项
        /// </summary>
        /// <param name="abName"></param>
        /// <returns></returns>
        public string[] RetrivalDependence(string abName)
        {
            if(_ManifestObj!= null && !string.IsNullOrEmpty(abName))
            {
                return _ManifestObj.GetAllDependencies(abName);
            }
            return null;
        }

        /// <summary>
        /// 最后一个再调用
        /// 释放本类资源
        /// </summary>
        public void Dispose()
        {
            if (_ABReadManifest != null)
            {
                _ABReadManifest.Unload(true);
            }
        }
    }
}
